﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SwitchWebApp.Models;
using SwitchWebApp;
using System.ServiceModel;
using SwitchWebApp.SwitchServiceRef;
using SwitData;
namespace SwitchWebApp.Controllers
{
    [Authorize]
    public class SwitchController : Controller
    {
        private SwitchWebAppContext db = new SwitchWebAppContext();
        private SwitchServiceClient service = new SwitchServiceClient();
        //
        // GET: /Switch/

        public ActionResult Index()
        {
            return View(service.GetSwitchList());            
        }

        //
        // GET: /Switch/Details/5

        public ActionResult Details(int id = 0)
        {
            SwitData.PortableSwitch switchmodel = service.GetSwitch(id);
            if (switchmodel == null)
            {
                return HttpNotFound();
            }
            return View(switchmodel);
        }

        //
        // GET: /Switch/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Switch/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(SwitData.PortableSwitch switchmodel)
        {
            if (ModelState.IsValid)
            {
                service.AddSwitch(switchmodel);
                return RedirectToAction("Index");
            }

            return View(switchmodel);
        }

        //
        // GET: /Switch/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SwitData.PortableSwitch switchmodel = service.GetSwitch(id);
            if (switchmodel == null)
            {
                return HttpNotFound();
            }
            return View(switchmodel);
        }

        //
        // POST: /Switch/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(SwitData.PortableSwitch switchmodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(switchmodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(switchmodel);
        }

        //
        // GET: /Switch/Delete/5

        public ActionResult Delete(int id = 0)
        {
            SwitData.PortableSwitch switchmodel = service.GetSwitch(id);
            if (switchmodel == null)
            {
                return HttpNotFound(); 
            }
            return View(switchmodel);
        }

        //
        // POST: /Switch/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            service.DeleteSwitch(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}